package com.example.java.io;

import com.xhf.tools.KMPUtil;

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class BinaryFileStreamingReadExample {
    private static final int BUFFER_SIZE = 2048; // 缓冲区大小，可根据需求调整

    private static Boolean isJs = false;
    private static Boolean isPKG = false;
    private static Boolean isDependencies = false;


    private static Boolean isRustC = false;
    private static Boolean isRustAlloc = false;
    private static Boolean isRustPanic = false;

    private static Map<String, Object> map = new HashMap<>();

    public static void main(String[] args) {
        long start = System.currentTimeMillis();
        FileInputStream inputStream = null;
        try {
//            String path = "/Users/xiaohongfei/Documents/workdoc/testCase/binary/rust/rust-play.exe";
//            String path  = "/Users/xiaohongfei/Documents/workdoc/testCase/binary/go/kube-apiserver-healthcheck.exe";
            String path  = "/Users/xiaohongfei/Documents/workdoc/testCase/binary/js/js-beautify.exe";
//            String path = "/Users/xiaohongfei/Documents/workdoc/testCase/binary/7zexe/7z-pom.exe";
//            String path  = "/Users/xiaohongfei/Documents/workdoc/testCase/binary/winrarexe/winrar-rar4-pom.exe";


            inputStream = new FileInputStream(path);
            byte[] buffer = new byte[BUFFER_SIZE];
            byte[] bufferTemp = new byte[128];
            byte[] bufferResult = new byte[BUFFER_SIZE + 128];
            int bytesRead;
            String str = "";
            while ((bytesRead = inputStream.read(buffer)) != -1) {
                System.arraycopy(bufferTemp, 0, bufferResult, 0, bufferTemp.length);
                System.arraycopy(buffer, 0, bufferResult, bufferTemp.length, buffer.length);

                System.arraycopy(buffer, BUFFER_SIZE - 128, bufferTemp, 0, bufferTemp.length);

                // 在这里处理读取的数据
                // 使用buffer数组中的前bytesRead个字节
                processRustDependencyBytes(buffer, bytesRead);
                processGoBytes(buffer, bytesRead);
                processWinRarBytes(buffer, bytesRead);
                processJsBytes(buffer, bytesRead);
                process7zBytes(buffer, bytesRead);
                processRustBytes(bufferResult, bytesRead);

            }

            //js文件识别
            if (isJs && isPKG && isDependencies) {
                System.out.println("这是一个js的二进制文件");
            }

            if (isRustAlloc && isRustPanic && isRustC) {
                System.out.println("这是一个rust的二进制文件");
            }



           /* String regex = "[\\\\/]([\\w-]+)-([\\d.]+)[\\\\/]";
            Pattern pattern = Pattern.compile(regex);
            final Matcher matcher = pattern.matcher(str);
            while (matcher.find()) {

                String name = matcher.group(1);
                String version = matcher.group(2);

                System.out.println(name + version);
            }*/

            long end = System.currentTimeMillis();
            System.out.println("=====耗时:" + (end - start) + "ms");

//            System.out.println(str);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    private static String processRustDependencyBytes(byte[] buffer, int bytesRead) {

        String str = new String(buffer, 0, bytesRead);

        String regex = "[\\\\/]([\\w-]+)-([\\d.]+)[\\\\/]";
        Pattern pattern = Pattern.compile(regex);
        final Matcher matcher = pattern.matcher(str);
        while (matcher.find()) {

//            System.out.println(str);
            String name = matcher.group(1);
            String version = matcher.group(2);
            if (map.containsKey(name + version))
                return "";
            map.put(name + version, null);
            System.out.println(name + version);
        }
        return "";
    }

    private static String processGoBytes(byte[] buffer, int bytesRead) {
        int isGoPE = KMPUtil.search(buffer, "Go build ID:".getBytes());
        if (isGoPE != -1) {
            System.out.println("go文件");
        }
        return "";
    }

    private static String process7zBytes(byte[] buffer, int bytesRead) {
        int is7z = KMPUtil.search(buffer, ".sxdata".getBytes());
        if (is7z != -1) {
            System.out.println("7z文件");
        }
        return "";
    }

    private static String processWinRarBytes(byte[] buffer, int bytesRead) {
        int isWinrar = KMPUtil.search(buffer, "D:\\Projects\\WinRAR\\sfx\\build".getBytes());
        if (isWinrar != -1) {
            System.out.println(true);
        }
        return "";
    }

    private static String processJsBytes(byte[] buffer, int bytesRead) {

        int isJsInt = KMPUtil.search(buffer, "node.js".getBytes());
        if (isJsInt != -1) {
            isJs = true;
        }
        int isPKGInt = KMPUtil.search(buffer, "PKG".getBytes());
        if (isPKGInt != -1) {
            isPKG = true;
        }
        int isDependenciesInt = KMPUtil.search(buffer, "dependencies".getBytes());
        if (isDependenciesInt != -1) {
            isDependencies = true;
        }
        return "";
    }

    private static String processRustBytes(byte[] buffer, int bytesRead) {

        int isRustCInt = KMPUtil.search(buffer, "rustc".getBytes());
        if (isRustCInt != -1) {
            isRustC = true;
        }
        int isRustAllocInt = KMPUtil.search(buffer, "alloc.rs".getBytes());
        if (isRustAllocInt != -1) {
            isRustAlloc = true;
        }
        int isRustPanicInt = KMPUtil.search(buffer, "panic.rs".getBytes());
        if (isRustPanicInt != -1) {
            isRustPanic = true;
        }
        return "";
    }
}
